package com.example.android.raycast;

import android.content.Context;
import android.os.Bundle;

public class Player {

	public static int ROTATE_LEFT = -1;
	public static int ROTATE_NONE = 0;
	public static int ROTATE_RIGHT = 1;
	public static int WALK_FORWARD = -1;
	public static int WALK_NONE = 0;
	public static int WALK_BACKWARD = 1;

	//Constants
	private int mStartX;// =  (int) (2.5 * 64); //8.5;//160; = 2.5 * 64
	private int mStartY;//PLAYER_START_Y =  (int) (1.5 * 64); //3.5;//96; = 1.5 * 64
	//private final int PLAYER_HEIGHT = 32;
	private final int PLAYER_WALK_SPEED = 16;

	// Keys for storing/restoring game state when paused
	private static final String KEY_HEADING = "mAngle";
	private static final String KEY_PLAYERX = "mX";
	private static final String KEY_PLAYERY = "mY";
	private static final String KEY_ROTATING = "mRotating";
	private static final String KEY_WALKING = "mWalking";
	private static final String KEY_HEALTH = "mHealth";
	private static final String KEY_FIRING = "mFiring";
	//private static final String KEY_WEAPON = "mCurrentWeaponType";
	
	//Player attributes
	private int mRotating = ROTATE_NONE;
	private int mWalking = WALK_NONE;
	private boolean mFiring = false;
	//private int mCurrentWeaponType = 1;
	private int mHealth = 200;	
	private int mX = mStartX;
	private int mY = mStartY;
	private int mAngle; //was ANGLE130
	private Weapon mWeapon;
	//private int mCanvasWidth;
	
	//angles - these can change depending on the width of the canvas (0 is fixed)
	private final int ANGLE0 = 0;
	private int ANGLE360 = 360;
	private int ANGLE90 = 90;
	private int PLAYER_ROTATE_SPEED = 0;
	
	public Player(int weaponType, int startX, int startY, int canvasWidth, int canvasHeight, Context context) {
		
		int angle60 = canvasWidth;
		int angle30 = (angle60 / 2);
		ANGLE90 = (angle30 * 3);
		ANGLE360 = (angle60 * 6);
		int angle10 = (angle60 / 6);
		PLAYER_ROTATE_SPEED = angle10;		
		mAngle = ANGLE90;	
		
		//mCanvasWidth = canvasWidth;
		mWeapon = new Weapon(weaponType, canvasWidth, canvasHeight, context);
		
		mStartX = startX;
		mStartY = startY;
		mX = mStartX;
		mY = mStartY;

	}
	
	public void useNewLevelStartPositions(int startX, int startY) {
		mStartX = startX;
		mStartY = startY;
		mX = mStartX;
		mY = mStartY;
	}
	
	public void updateAngles(int canvasWidth, int canvasHeight) {
		int angle60 = canvasWidth;
		int angle30 = (angle60 / 2);
		ANGLE90 = (angle30 * 3);
		ANGLE360 = (angle60 * 6);
		int angle10 = (angle60 / 6);
		PLAYER_ROTATE_SPEED = angle10;		
		mAngle = ANGLE90;	
		
		//mCanvasWidth = canvasWidth;
		mWeapon.updateCanvasWidth(canvasWidth, canvasHeight);
	}
	
	public void restoreState(Bundle savedState) {		
		mX = savedState.getInt(KEY_PLAYERX);
		mY = savedState.getInt(KEY_PLAYERY);
		mAngle = savedState.getInt(KEY_HEADING);
		mRotating = savedState.getInt(KEY_ROTATING);
		mWalking = savedState.getInt(KEY_WALKING);
		mHealth = savedState.getInt(KEY_HEALTH);
		mFiring = savedState.getBoolean(KEY_FIRING);
		//mCurrentWeaponType = savedState.getInt(KEY_WEAPON);
		//TODO restore mWeapon
	}
	
	public Bundle saveState(Bundle map) {		
		map.putInt(KEY_HEADING, Integer.valueOf(mAngle));
		map.putInt(KEY_PLAYERX, Integer.valueOf(mX));
		map.putInt(KEY_PLAYERY, Integer.valueOf(mY));
		map.putInt(KEY_ROTATING, Integer.valueOf(mRotating));
		map.putInt(KEY_WALKING, Integer.valueOf(mWalking));
		map.putInt(KEY_HEALTH, Integer.valueOf(mHealth));
		map.putBoolean(KEY_FIRING, Boolean.valueOf(mFiring));
		//map.putInt(KEY_WEAPON, Integer.valueOf(mCurrentWeaponType));
		//TODO save mWeapon

		return map;
	}

	public void stop() {
		mRotating = ROTATE_NONE;
		mWalking = WALK_NONE;
		mFiring = false;		
	}
	
	public void setFiring(boolean firing) {
		mFiring = firing;
	}
	
	public void setWalking(int direction) {
		mWalking = direction;
	}
	
	public void setRotating(int direction) {
		mRotating = direction;
	}

	public boolean getFiring() {
		return mFiring;
	}
	
	public Weapon getWeapon() {
		return mWeapon;
	}

	public int getHealth() {
		return mHealth;
	}
	
	public int getAngle() {
		return mAngle;
	}
	
	public int getWalking() {
		return mWalking;
	}
	
	public int getWalkingSpeed() {
		return PLAYER_WALK_SPEED;
	}
	
	public void updatePosition(int newX, int newY) {
		mX = newX;
		mY = newY;
	}

	public void doRotation() {
		// ensure angle remains between ANGLE0 and ANGLE360
		if (mRotating == ROTATE_LEFT) {
			if ((mAngle -= PLAYER_ROTATE_SPEED) < ANGLE0) {
				mAngle += ANGLE360;
			}
		} else if (mRotating == ROTATE_RIGHT) {
			if ((mAngle += PLAYER_ROTATE_SPEED) >= ANGLE360) {
				mAngle -= ANGLE360;
			}
		}
		
	}

	public void hit(int damage) {
		mHealth -= damage;
		
	}

	public int getX() {
		return mX;
	}
	
	public int getY() {
		return mY;
	}

	public void reset() {
		mRotating = ROTATE_NONE;
		mWalking = WALK_NONE;
		mFiring = false;
		//mCurrentWeaponType = 1;
		mHealth = 100;	
		mX = mStartX;
		mY = mStartY;
		mAngle = ANGLE90;
		mWeapon.reset();
		
	}

	public void destroy() {
		mWeapon.destroy();		
	}

	public void pickUpItem(int itemType) {
		if(itemType == Level.HEALTH_ITEM) {
			mHealth += 10;
		} else if(itemType == Level.AMMO_ITEM) {
			mWeapon.ammoPickup();
		}
		
	}
}
